package neusoft.yuejian_11.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.EventBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.EventModel;
import neusoft.yuejian_11.R;

public class EventDetailActivity extends BaseActivity implements RetrofitListener{
    protected Context context;
    private int activityId;
    private String activityName;
    private String activityPhone;
    private String activityPicture;
    private TextView textName;
    private ImageButton imageButtonBack;
    private ImageView imageView;
    private TextView textInfo;
    private TextView textAddress;
    private TextView textPrice;
    private TextView textTime;
    private TextView textPhone;
    private TextView textNumber;
    private Button buttonActivityn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_detail);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        textName = (TextView) findViewById(R.id.textView_event_name);
        imageView = (ImageView) findViewById(R.id.imageView);
        textPhone = (TextView) findViewById(R.id.text_event_phone);

        textAddress = (TextView) findViewById(R.id.text_event_address);
        textPrice = (TextView) findViewById(R.id.text_event_price);
        textTime = (TextView) findViewById(R.id.text_event_time);
        textNumber = (TextView) findViewById(R.id.text_event_number);
        textInfo = (TextView) findViewById(R.id.text_event_info);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        buttonActivityn = (Button) findViewById(R.id.button_activityn);
    }

    private void showData() {
        activityId = getIntent().getIntExtra("a_id", -1);
        activityName = getIntent().getStringExtra("a_name");
        textName.setText(activityName);
        activityPicture = getIntent().getStringExtra("a_picture");
        Picasso.with(context)
                .load(activityPicture)
                .into(imageView);
        activityPhone = getIntent().getStringExtra("a_phone");
        textPhone.setText(activityPhone);
    }

    private void initEvents() {
        buttonActivityn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(EventDetailActivity.this, EventnActivity.class);
                intent.putExtra("a_id",activityId);
                startActivity(intent);
            }
        });

        textPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + activityPhone));
                if (ActivityCompat.checkSelfPermission(EventDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initData() {
        EventModel eventModel = new EventModel();
        eventModel.eventDetail(String .valueOf(activityId),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        EventBean eventModelDetail = (EventBean) o;
        textInfo.setText(eventModelDetail.getA_info());
        textAddress.setText(eventModelDetail.getA_address());
        textPrice.setText(String.valueOf(eventModelDetail.getA_price()));
        textTime.setText(eventModelDetail.getA_time());
        textNumber.setText(String.valueOf(eventModelDetail.getA_number()));

    }

    @Override
    public void onFail() {

    }
}
